package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hjq.toast.ToastUtils;
import com.qiantu.phone.R;
import com.lxj.xpopup.core.BottomPopupView;

public class BottomAvatarDialog extends BottomPopupView {

    private DialogClickListener mDialogClickListener;
    private ImageView avatarMale1;
    private ImageView avatarMale2;
    private ImageView avatarGirl1;
    private ImageView avatarGirl2;
    private View avatarMale1Layout;
    private View avatarMale2Layout;
    private View avatarGirl1Layout;
    private View avatarGirl2Layout;
    private View btnStartCamera;
    private View btnStartGallery;

    public BottomAvatarDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_avatar;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
//        avatarMale1 = findViewById(R.id.avatar_male1);
//        avatarMale2 = findViewById(R.id.avatar_male2);
//        avatarGirl1 = findViewById(R.id.avatar_girl1);
//        avatarGirl2 = findViewById(R.id.avatar_girl2);
        avatarMale1Layout = findViewById(R.id.avatar_male1_layout);
        avatarMale2Layout = findViewById(R.id.avatar_male2_layout);
        avatarGirl1Layout = findViewById(R.id.avatar_girl1_layout);
        avatarGirl2Layout = findViewById(R.id.avatar_girl2_layout);
        btnStartCamera = findViewById(R.id.btn_start_camera);
        btnStartGallery = findViewById(R.id.btn_start_gallery);
        View btnSave = findViewById(R.id.btn_save);
        MyOnClickListener myOnClickListener = new MyOnClickListener();
        avatarMale1Layout.setOnClickListener(myOnClickListener);
        avatarMale2Layout.setOnClickListener(myOnClickListener);
        avatarGirl1Layout.setOnClickListener(myOnClickListener);
        avatarGirl2Layout.setOnClickListener(myOnClickListener);
        btnSave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if(mDialogClickListener != null){
                    int selected = 0;
                    if(avatarMale1Layout.isSelected()){
                        selected = 1;
                    }else if(avatarMale2Layout.isSelected()){
                        selected = 2;
                    }else if(avatarGirl1Layout.isSelected()){
                        selected = 3;
                    }else if(avatarGirl2Layout.isSelected()){
                        selected = 4;
                    }
                    if(selected == 0){
                        ToastUtils.show(R.string.please_select_avatar);
                        return;
                    }
                    mDialogClickListener.onConfirmClick(selected);
                }
            }
        });
        btnStartCamera.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mDialogClickListener != null){
                    mDialogClickListener.onStartCamera();
                }
            }
        });
        btnStartGallery.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mDialogClickListener != null){
                    mDialogClickListener.onStartGallery();
                }
            }
        });
    }

    private class MyOnClickListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.avatar_male1_layout:
                    avatarMale1Layout.setSelected(true);
                    avatarMale2Layout.setSelected(false);
                    avatarGirl1Layout.setSelected(false);
                    avatarGirl2Layout.setSelected(false);
                    break;
                case R.id.avatar_male2_layout:
                    avatarMale1Layout.setSelected(false);
                    avatarMale2Layout.setSelected(true);
                    avatarGirl1Layout.setSelected(false);
                    avatarGirl2Layout.setSelected(false);
                    break;
                case R.id.avatar_girl1_layout:
                    avatarMale1Layout.setSelected(false);
                    avatarMale2Layout.setSelected(false);
                    avatarGirl1Layout.setSelected(true);
                    avatarGirl2Layout.setSelected(false);
                    break;
                case R.id.avatar_girl2_layout:
                    avatarMale1Layout.setSelected(false);
                    avatarMale2Layout.setSelected(false);
                    avatarGirl1Layout.setSelected(false);
                    avatarGirl2Layout.setSelected(true);
                    break;
            }
        }
    }

    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    public void setDialogClickListener(@Nullable DialogClickListener listener) {
        mDialogClickListener = listener;
    }

    public interface DialogClickListener {
        void onConfirmClick(int selected);
        void onStartCamera();
        void onStartGallery();
    }
}
